//
//  TL_ProductionCoverView.m
//  WXReader
//
//  Created by Andrew on 2020/7/17.
//  Copyright © 2020 Andrew. All rights reserved.
//

#import "TL_ProductionCoverView.h"

@implementation TL_ProductionCoverView
{
    // 漫画
    UIImageView *coverBottomTitleImageView;
    UILabel *coverBottomTitleLabel;
    UIView *coverLockView;
    UIImageView *coverLockIcon;
    
    TL_ProductionType _productionType;
    TL_ProductionCoverDirection _productionCoverDirection;
}

- (instancetype)initWithProductionType:(TL_ProductionType)productionType productionCoverDirection:(TL_ProductionCoverDirection)productionCoverDirection
{
    if (self = [super init]) {
        
        _productionType = productionType;
        _productionCoverDirection = productionCoverDirection;
        
        [self createSubview];
    }
    return self;
}

- (void)createSubview
{
    // 作品图片
    self.productionImageView = [[YYAnimatedImageView alloc] init];
    self.productionImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.productionImageView.clipsToBounds = YES;
    self.productionImageView.userInteractionEnabled = YES;
    self.productionImageView.layer.cornerRadius = 6.0;
    [self addSubview:self.productionImageView];
    [self resetDefaultHoldImage];
    
    // 漫画锁标志
    coverLockView = [[UIView alloc] init];
    coverLockView.userInteractionEnabled = YES;
    coverLockView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    coverLockView.hidden = YES;
    [self addSubview:coverLockView];
    
    coverLockIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"comic_lock"]];
    [coverLockView addSubview:coverLockIcon];
    
    // 漫画底部标题背景
    coverBottomTitleImageView = [[UIImageView alloc] init];
    coverBottomTitleImageView.hidden = YES;
    coverBottomTitleImageView.image = [UIImage imageNamed:@"public_mask_bottom"];
    [self addSubview:coverBottomTitleImageView];
    
    // 漫画底部标题
    coverBottomTitleLabel = [[UILabel alloc] init];
    coverBottomTitleLabel.hidden = YES;
    coverBottomTitleLabel.textColor = UIColor.whiteColor;
    coverBottomTitleLabel.textAlignment = NSTextAlignmentRight;
    coverBottomTitleLabel.backgroundColor = [UIColor clearColor];
    coverBottomTitleLabel.font = SYSTEM_FONT(12);
    [self addSubview:coverBottomTitleLabel];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.productionImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    
    [coverLockView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.productionImageView);
    }];
    
    [coverLockIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(12);
        make.centerX.mas_equalTo(coverLockView.mas_centerX);
        make.centerY.mas_equalTo(coverLockView.mas_centerY);
    }];
    
    [coverBottomTitleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.productionImageView.mas_left);
        make.bottom.mas_equalTo(self.productionImageView.mas_bottom);
        make.width.mas_equalTo(self.productionImageView.mas_width);
        make.height.mas_equalTo(40);
    }];
    
    [coverBottomTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.productionImageView).offset(kQuarterMargin);
        make.right.mas_equalTo(self.productionImageView).offset(-kQuarterMargin);
        make.bottom.mas_equalTo(self.productionImageView);
        make.height.mas_equalTo(30);
    }];
}

// 重置为默认图
- (void)resetDefaultHoldImage
{
    if (_productionCoverDirection == TL_ProductionCoverDirectionHorizontal) {
        self.productionImageView.image = [UIImage imageNamed:@"public_placeholderImage_horizontal"];
    }
    
    if (_productionCoverDirection == TL_ProductionCoverDirectionVertical) {
        self.productionImageView.image = [UIImage imageNamed:@"public_placeholderImage_vertical"];
    }
}

- (void)setIs_locked:(BOOL)is_locked
{
    _is_locked = is_locked;
    
    if (_productionType == TL_ProductionTypeComic) {
        coverLockView.hidden = !_is_locked;
    } else {
        coverLockView.hidden = YES;
    }
}

- (void)setProductionType:(TL_ProductionType)productionType
{
    _productionType = productionType;
    
}

- (void)setProductionCoverDirection:(TL_ProductionCoverDirection)productionCoverDirection
{
    _productionCoverDirection = productionCoverDirection;
    
    [self resetDefaultHoldImage];
}

- (void)setCoverImageURL:(NSString *)coverImageURL
{
    _coverImageURL = coverImageURL;
    
    if (_productionCoverDirection == TL_ProductionCoverDirectionHorizontal) {
        [self.productionImageView setImageWithURL:[NSURL URLWithString:coverImageURL?:@""] placeholder:[UIImage imageNamed:@"public_placeholderImage_horizontal"]];
    } else {
        [self.productionImageView setImageWithURL:[NSURL URLWithString:coverImageURL?:@""] placeholder:[UIImage imageNamed:@"public_placeholderImage_vertical"]];
    }
}

- (void)setCoverTitleString:(NSString *)coverTitleString
{
    _coverTitleString = coverTitleString;
    
    if (coverTitleString.length > 0) {
        coverBottomTitleImageView.hidden = NO;
        
        coverBottomTitleLabel.hidden = NO;
        coverBottomTitleLabel.text = coverTitleString;
    }
}

@end
